const sctv_schema = {
    // 正式环境测试账号
    'eDAwMDI=': {
        ios: 'mianyang',
        android: 'pengxi'
    },
    // 本地测试环境
    'eDAwMDE=': 'pengxi',
    // 蓬溪
    'cDA5MjE=': 'pengxi',
    // 绵阳
    'YjA3MDA=': 'mianyang',
    // 熊猫视频
    'eDAxMTE=': 'xiongmaoshipin',
    // 武胜
    'dzE2MjI=': 'wusheng',
    // 三台
    'YTA3MjI=': 'santai',
    // 游仙
    'YTA3MDQ=': 'youxian',
    // 盐边
    'YTA0MjI=': 'yanbian',
    // 广汉
    'ZzA2ODE=': 'guanghan',
    // 旌阳
    'ajA2MDM=': 'jingyang',
    // 安居
    'YTA2MTY=': 'anju',
    // 高县
    'ZzE1MjU=': 'gaoxian',
    // 青川
    'YTA4MjI=': 'qingchuan',
    // 简阳
    'ajAxODU=': 'jianyang',
    // 隆昌
    'bDEwODM=': 'longchang',
    // 美姑
    'YTM0MzY=': 'meigu',
    // 东兴
    'YTEwMTE=': 'dongxing',
    // 普格
    'YTM0Mjg=': 'puge',
    // 安州
    'YTA3MDU=': 'anzhou',
    // 德昌
    'YTM0MjQ=': 'dechang',
    // 盐源
    'YTM0MjM=': 'yanyuan',
    // 江油
    'YTA3ODE=': 'jiangyou',
    // 巴州
    'YjM0NTg=': 'bazhou',
    // 昭化
    'YTA4MTE=': 'zhaohua',
    // 梓潼
    'YTA3MjU=': 'zitong',
    // 盐亭
    'YTA3MjM=': 'yanting',
    // 合江
    'bDA1MjI=': 'hejian',
    // 五通桥
    'bDExMTI=': 'wutongqian',
    // 旺苍
    'YTA4MjE=': 'wangcang',
    // 冕宁
    'YTM0MzM=': 'mianning',
    // 屏山
    'QTE1Mjk=': 'pingshan',
    // 四川乡村
    'YzAxMTE=': 'sichuanxc',
    // 罗江
    'YTA2MDQ=': 'luojiang'
}

// 设备信息
const deviceInfo = (function () {
    const ua = navigator.userAgent.toLowerCase();
    return {
        // iOS
        isIOS: /iphone|ipad|ipod|ios/.test(ua),
        // android
        isAndroid: /android/.test(ua),
        // 微信
        isWechat: /micromessenger/.test(ua),
        // 微博
        isWeibo: /weibo/.test(ua),
        // QQ
        isQQ: /qq\//.test(ua),
        // QQ浏览器
        isQQBrowser: /mqqbrowser/.test(ua)
    }
})()

/**
 * 打开下载地址
 * @param {Number} jumpContentType 跳转内容类型
 */
function openDownloadUrl(jumpContentType) {
    // 判断app
    if (deviceInfo.isWechat // 微信
        ||
        deviceInfo.isWeibo // 微博
        ||
        deviceInfo.isQQ //  QQ
    ) {
        openInBrowserHint()
    } else {
        openOrDownloadApp(jumpContentType)
    }
}

/**
 * 使用浏览器中打开提示
 */
function openInBrowserHint() {
    const body = document.body
    const mask = document.createElement('div')
    const cueLine = document.createElement('img')
    const hint = document.createElement('div')
    mask.style = 'position: fixed; top: 0; left: 0; bottom: 0; right: 0; background: rgba(0, 0, 0, 0.8); display: flex; flex-direction: column; align-items: flex-end; z-index: 999;'
    cueLine.src = './../../../../js_css/images/cueline.png'
    cueLine.style = 'width: 122px; height: 125px; margin: 10px 20px 0 0'
    hint.innerHTML = '请点击右上角，<br/>使用浏览器打开此页面。'
    hint.style = 'width: 100%; font-size: 16px; color: rgba(255, 255, 255, 0.9); line-height: 21px; display: flex; flex-direction: column; align-items: center; margin-top: -14px;'
    mask.appendChild(cueLine)
    mask.appendChild(hint)
    body.appendChild(mask)
}

/**
 * 转到下载页面
 */
function gotoDownloadPage() {
    let downloadUrl;
    if (deviceInfo.isIOS) {
        downloadUrl = document.getElementById('downloadUrlOfIOS').innerText.trim()
    } else {
        downloadUrl = document.getElementById('downloadUrlOfAndroid').innerText.trim()
    }
    window.location.href = downloadUrl
}

/**
 * 打开或下载APP
 * @param {Number} jumpContentType 跳转内容类型
 */
function openOrDownloadApp(jumpContentType) {
    const protocolMap = {
        1: '{{schema}}://sctvcloud.com/news/detail?jumpId={{jumpId}}&jumpType={{jumpType}}&jumpContentType={{jumpContentType}}&jumpUrl={{jumpUrl}}',
        2: '{{schema}}://sctvcloud.com/video/detail?jumpId={{jumpId}}&jumpType={{jumpType}}&jumpContentType={{jumpContentType}}&jumpUrl={{jumpUrl}}',
        3: '{{schema}}://sctvcloud.com/news/live_detail?jumpId={{jumpId}}&jumpType={{jumpType}}&jumpContentType={{jumpContentType}}&jumpUrl={{jumpUrl}}',
        4: '{{schema}}://sctvcloud.com/news/atlas?jumpId={{jumpId}}&jumpType={{jumpType}}&jumpContentType={{jumpContentType}}&jumpUrl={{jumpUrl}}',
        5: '{{schema}}://sctvcloud.com/news/detailH5?jumpId={{jumpId}}&jumpType={{jumpType}}&jumpContentType={{jumpContentType}}&jumpUrl={{jumpUrl}}',
        15: '{{schema}}://sctvcloud.com/news/topic?jumpId={{jumpId}}&jumpType={{jumpType}}&jumpContentType={{jumpContentType}}&jumpUrl={{jumpUrl}}',
        23: '{{schema}}://sctvcloud.com/news/headline?jumpId={{jumpId}}&jumpType={{jumpType}}&jumpContentType={{jumpContentType}}&jumpUrl={{jumpUrl}}',
        25: '{{schema}}://sctvcloud.com/platform/apply/detail?jumpId={{jumpId}}&jumpType={{jumpType}}&jumpContentType={{jumpContentType}}&jumpUrl={{jumpUrl}}',
        26: '{{schema}}://sctvcloud.com/platform/report/detail?jumpId={{jumpId}}&jumpType={{jumpType}}&jumpContentType={{jumpContentType}}&jumpUrl={{jumpUrl}}',
        29: '{{schema}}://sctvcloud.com/platform/social/detail?jumpId={{jumpId}}&jumpType={{jumpType}}&jumpContentType={{jumpContentType}}&jumpUrl={{jumpUrl}}'
    }
    const params = JSON.parse(document.getElementById("params-data").innerText.trim())
    let schema = sctv_schema[params.tenant]
    if (typeof (schema) === 'object') {
        schema = schema[deviceInfo.isIOS ? 'ios' : 'android']
    }
    if (schema) {
        const jumpId = params.id
        const jumpType = 2
        const jumpUrl = window.location.href.replace(window.location.origin, '')
        // 通过协议打开APP
        if(protocolMap[jumpContentType])
        window.location.href = protocolMap[jumpContentType]
            .replace('{{schema}}', schema)
            .replace('{{jumpId}}', jumpId)
            .replace('{{jumpType}}', jumpType)
            .replace('{{jumpContentType}}', jumpContentType)
            .replace('{{jumpUrl}}', jumpUrl)
        // 定时转至下载连接
        setTimeout(function () {
            if (window.confirm('确定要打开下载地址吗？')) {
                gotoDownloadPage()
            }
        }, (function () {
            // IOS上QQ浏览器在未安装APP时也会提示是否打开APP，点击打开无响应
            // 暂解决IOS上QQ浏览器是否打开APP提示被确认下载对话框覆盖的问题，默认浏览器和android上的浏览器不会有该问题。
            if (deviceInfo.isIOS && deviceInfo.isQQBrowser) {
                return 5000
            }
            return 500
        })())
    } else {
        gotoDownloadPage()
    }
}